package org.hefan.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hefan.basic.util.AjaxResult;
import org.hefan.basic.util.PageList;
import org.hefan.org.domain.Department;
import org.hefan.org.query.DepartmentQuery;
import org.hefan.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类
 */
@RestController
@RequestMapping("/department")
@Api(tags = "宠物之家接口",description = "宠物之家接口详细描述")
public class DepartmentController {

    /**
     * 注入departmentService
     */
    @Autowired
    private IDepartmentService departmentService;

    /**
     * //处理请求：Get http://localhost:8080/department
     * 查询全部
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询全部部门",notes = "")
    public List<Department> findAll() {
        return departmentService.loadAll();
    }

    /**
     * 处理请求：Get http://localhost:8080/department/1
     * 查询单个Department部门对象
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个部门",notes = "需要传入id")
    public Department findOne(@PathVariable("id")Long id) {
        return departmentService.loadById(id);
    }

    /**
     * 分页查询
     * post请求,参数在请求体中,通过@RequestBody获取请求体中的参数
     * @param departmentQuery 分页查询参数
     * @return
     */
    @PostMapping
    @ApiOperation(value = "通过分页查询部门",notes = "需要传入分页数据")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery) {
        return departmentService.queryPage(departmentQuery);
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除部门",notes = "通过id删除")
    public AjaxResult delete(@PathVariable("id")Long id) {
        try {
            departmentService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    /**
     * 添加或修改部门
     * @param department
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改部门",notes = "有id则为修改,没有id则是添加")
    public AjaxResult saveOrUpdate(@RequestBody Department department) {
        try {
            if (department.getId() == null) {
                departmentService.save(department);
            } else {
                departmentService.update(department);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    /**
     * 批量删除部门
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除部门",notes = "传需要批量删除的部门id")
    public AjaxResult batchRemove(@RequestBody List<Long> ids) {
        try {
            departmentService.batchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    /**
     * 查询部门树
     * @return
     */
    @GetMapping("/deptTree")
    @ApiOperation(value = "查询部门树",notes = "")
    public List<Department> getDeptTree() {
        return departmentService.getDeptTree();
    }
}
